package cn.bonoon.kernel.web.components;

import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.annotations.components.AsNumberBox;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;
import cn.bonoon.kernel.web.html.form.ComponentPropertyParser;

public class NumberBoxComponent extends UIComponent {
	private final double max, min, precision;
	private final String prefix, suffix;
	public NumberBoxComponent(ComponentPropertyParser property, UIPropertyAccessor accessor, AsNumberBox asNumberBox) {
		super("bupnuminput", property, accessor);
		this.max = asNumberBox.max();
		this.min = asNumberBox.min();
		this.precision = asNumberBox.precision();
		this.prefix = asNumberBox.prefix();
		this.suffix = asNumberBox.suffix();
	}
	public NumberBoxComponent(ComponentPropertyParser property, UIPropertyAccessor accessor) {
		super("bupnuminput", property, accessor);
		this.max = Double.NaN;
		this.min = Double.NaN;
		this.precision = 0;
		this.prefix = null;
		this.suffix = null;
	}
	@Override
	protected void initComponentAttribute(StringBuilder script, UIPropertyBuilder builder) {
		if (!Double.isNaN(max)){
			script.append(",max:").append(max);
		}
		if (!Double.isNaN(min)){
			script.append(",min:").append(min);
		}
		if (precision > 0){
			script.append(",precision:").append(precision);
		}
		if(StringHelper.isNotEmpty(prefix)){
			script.append(",prefix:'").append(prefix).append('\'');
		}
		if(StringHelper.isNotEmpty(suffix)){
			script.append(",suffix:'").append(suffix).append('\'');
		}
	}
}
